<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Open Source Web Animation - C++ Tutorial: String</title>
  <meta
 content="Open Source Web Animation C++ Tutorial: String"
 name="description" />
  <meta
 content="Open Source, Web Animation, C++ Tutorial, string, c_str()"
 name="keywords" />
  <meta http-equiv="Content-Type"
 content="text/html; charset=ISO-8859-1" />
  <link type="text/css" rel="stylesheet" href="../images/bogostyleWidePre.css" />
</head>
<body>
<div id="page" align="center">
<div id="content" style="width: 800px;">
<div id="logo">
<div class="whitetitle" style="margin-top: 70px;">bogotobogo </div>
</div>
<div id="topheader">
<div class="headerbodytext" align="left"><br />
<strong>Bogotobogo</strong><br />
contact@bogotobogo.com </div>
<div class="smallgraytext" id="toplinks"><a href="../index.html">Home</a>
| <a href="../sitemap.html">Sitemap</a>
| <a href="../blog" target="_blank">Contact Us</a>
</div>
</div>
<div id="menu">
<div class="smallwhitetext" style="padding: 9px;" align="right"><a
 href="../index.html">Home</a>
| <a href="../about_us.html">About Us</a>
| <a href="../products.html">Products</a>
| <a href="../our_services.html">Our Services</a>
| <a href="../blog" target="_blank">Contact Us</a>
</div>
</div>
<div id="submenu">
<div class="smallgraytext" style="padding: 9px;" align="right"><a
 href="../gif.html">Gif</a> |
<a href="../java_applet.html">JavaApplet/Web Start</a>
| <a href="../flash.html">Flash</a>
| <a href="../shockwave.html">ShockWave</a>
| <a href="../svg.html">SVG</a>
| <a href="../iPhone.html">iPhone/iPad</a>
| <a href="../android.html">Android</a>
</div>
</div>

<div id="contenttext">

<!-- Use of this code assumes agreement with the Google Custom Search Terms of Service. -->
<!-- The terms of service are available at http://www.google.com/cse/docs/tos.html -->
<form name="cse" id="searchbox_demo" action="http://www.google.com/cse">
  <input type="hidden" name="cref" value="" />
  <input type="hidden" name="ie" value="utf-8" />
  <input type="hidden" name="hl" value="" />
  <input name="q" type="text" size="40" />
  <input type="submit" name="sa" value="Search" />
</form>
<script type="text/javascript" src="http://www.google.com/cse/tools/onthefly?form=searchbox_demo&lang="></script>

<div id="bookmarkshare">
<script type="text/javascript">var addthis_config = {"data_track_clickback":true};</script>
<a class="addthis_button" href="http://www.addthis.com/bookmark.php?v=250&amp;username=khhong7"><img src="http://s7.addthis.com/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a><script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#username=khhong7"></script>
</div>

<br />
<div style="padding: 10px;"><span class="titletext">C++ Tutorial - String</span></div>
<img src="../images/cplusplus/cpp_logo.jpg" alt="cplusplus logo"/>

<div class="bodytext" style="padding: 12px;" align="justify"> 
<div class="subtitle_2nd" id="FullList">Full List of C++ Tutorials</div>
<ul>
   <li><a href="cpptut.html">C++ Home</a> </li>
   <li><a href="string.html">String</a> </li>
   <li><a href="constructor.html">Constructor</a> </li>
   <li><a href="operatoroverloading.html">Operator Overloading</a> </li>
   <li><a href="virtualfunctions.html">Virtual Functions</a></li>
</ul>
</div>



<div class="bodytext" style="padding: 12px;" align="justify">
<div class="subtitle" id="Cstring">String</div>
<p>C++ has two types of string: <br />
the C-style character string and the C++ &lt;string&gt; class which is Standard C++ string.</p>
<br />
<br />
<div class="subtitle" id="Cstring">C string</div>
<p>Though the Standard C library is included as a part of Standard C++,
to use C library, we need to include the C header file:</p>
<pre>
	#include &lt;cstring&gt;
</pre>
<p>C string is stored as a character array. We manipulate the C string using a pointer. For example,<p>
<pre>
	const char *str ="I am a string";
</pre>
<p>we traverse it one by one each character:</p>
<pre>
	while(*str++){ }
</pre>
<p>The char* pointer str is dereferenced, and the character addressed is checked if it's true or false(if it's null).</p>
<p>OK! <br />
Let's find out what' the problem. </p>
<p><strong>Question 1</strong>
<pre>
	char *cstr1 = "hello";
	*(cstr1)='t';
</pre>
<p><strong>Question 2</strong>
<pre>

</pre>
<p><strong>Answer 1</strong>
<pre>
Unhandled exception at 0x00411baa in cstring.exe: 0xC0000005: Access violation writing location 0x0041783c.
</pre>
<p>Standard C library gives us a set of utility functions such as:</p>
<pre>
	/*returns the length of the string*/
	int strlen(const char*);

	/*copies the 2nd string into the 1st*/
	char* strcpy(char*, const char*);

	/*compares two strings*/
	int strcmp(const char*, const char*) 
</pre>
<p>Because we manipulate the C string using pointer which is low-level operation, it's error prone. That's why we have C++ &lt;string&gt; class.</p>
<br />
<div class="subtitle" id="Cplusstring">C++ string</div>
<p>Though there are lots of advantages of C++ string over C string, we won't talk about it at this time. But because numerous codes still using C style string, we need to look at the conversion between them. Here is an example with some error messages we'll get if we run it as it is.</p>
<pre>
#include &lt;string&gt;
#include &lt;cstring&gt;

int main()
{
	using namespace std;
	string str1;
	const char *pc = "I am just a character array";

	// C++ string type automatically convering a C character string
	// into a string object
	str1 = pc;   //ok

	// error C2440: 'initializing':
	// cannot convert from 'std::string' to 'char *'
	char *p1 = str1;  //not ok

	// error C2440: 'initializing' :
	// cannot convert from 'const char *' to 'char *'
	char *p2 = str1.c_str(); //not there yet

	const char *p3 = str1.c_str();  //ok

	return 0;
}
</pre> 
<p>The <strong>c_str()</strong> returns a pointer to <strong>const array</strong> in order to prevent the array from being directly manipulated. That's why we needs <strong>const</strong> qualifier in the example code.</p>

<div id="rightpanel">
<div align="center">
</div></div>

<br />
<br />

<div class="subtitle_2nd" id="FullList">Full List of C++ Tutorials</div>
<ul>
   <li><a href="cpptut.html">C++ Home</a> </li>
   <li><a href="string.html">String</a> </li>
   <li><a href="constructor.html">Constructor</a> </li>
   <li><a href="operatoroverloading.html">Operator Overloading</a> </li>
   <li><a href="virtualfunctions.html">Virtual Functions</a></li>
</ul>
<br />
<script type="text/javascript"><!--
google_ad_client = "pub-5767010733052716";
/* 300x250, created 7/22/10 */
google_ad_slot = "4543448457";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
<br />


<br />
<br />
<br />


</div>
</div>
<div class="smallgraytext" id="footer"><a href="../index.html">Home</a>
| <a href="../about_us.html">About Us</a>
| <a href="../products.html">products</a>
| <a href="../our_services.html">Our Services</a>
| <a href="#">Contact Us</a>
| Bogotobogo &copy; 2010 | <a target="_blank" href="http://www.bogotobogo.com">Bogotobogo </a>
</div>
</div>
</div>
</body>
</html>
